
package fileby.ctrl.transferhandlers.impl;

import fileby.model.IFileManager;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.TransferHandler;

/**
 *
 * @author Oscar
 */
class TabContentTransferHandler extends TransferHandler{

    private IFileManager fileManager;

    public TabContentTransferHandler(IFileManager fileManager) {
        this.fileManager = fileManager;
    }



        /*
         * This method indicates whether a component would accept an import
         * of the given set of data flavors prior to actually attempting to
         * import it. In this case we only support javaFileListFlavor.
         */
      @Override
        public boolean canImport(TransferHandler.TransferSupport support) {
            return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
        }

      /*
       *  Causes a transfer with files to a component from the DnD action.
       */
       @Override
       public boolean importData(TransferHandler.TransferSupport support) {
            Transferable t = support.getTransferable();
            try {
                List<File> fileList = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
                fileManager.addFiles(fileList, support.getDropAction());
            } catch (UnsupportedFlavorException e) {
                return false;
            } catch (IOException e) {
                return false;
            }
            return true;
        }

}
